package com.yoomiito.app.utils;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yoomiito.app.Constant;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.widget.dialog.NoNetDialog;

import java.util.HashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.cache.SharedPref;

/**
 * @author chenzhaojun
 * @date 2016/9/9 0009
 * @description
 */
public class NetworkUtils {
    public static final int NET_TYPE_WIFI = 0x01;
    public static final int NO_NETWORK = 0;
    public static final int NET_TYPE_CMWAP = 0x02;
    public static final int NET_TYPE_CMNET = 0x03;

    public static int getNetworkType() {
        int netType = NO_NETWORK;
        ConnectivityManager connectivityManager = (ConnectivityManager) App.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isAvailable()) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            String extraInfo = networkInfo.getExtraInfo();
            if (!TextUtils.isEmpty(extraInfo)) {
                if (extraInfo.toLowerCase().equals("cmnet")) {
                    netType = NET_TYPE_CMNET;
                } else {
                    netType = NET_TYPE_CMWAP;
                }
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = NET_TYPE_WIFI;
        }
        return netType;
    }

    public static boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) App.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnectedOrConnecting();
    }

    public static boolean checkNetPermission(Context context) {
//        if (!isNetworkConnected() && isForeground(context, ((BaseActivity) context).getLocalClassName())) {
//            NoNetDialog mNoNetDialog = new NoNetDialog(context);
//            if (!mNoNetDialog.isShowing()) {
//                mNoNetDialog.show();
//            }
//            return true;
//        }
//        return false;

        ConnectivityManager cm = (ConnectivityManager) App.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni!=null){
            if (ni.getType() == ConnectivityManager.TYPE_WIFI || ni.getType() == ConnectivityManager.TYPE_MOBILE) {
                return false;
            }
        }
        return true;
    }

    /**
     * 开启网络设置
     *
     * @param context
     */
    public static void startSetNet(Context context) {
        context.startActivity(new Intent(Settings.ACTION_SETTINGS));
    }

    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
        for (ActivityManager.RunningTaskInfo taskInfo : list) {
            if (taskInfo.topActivity.getShortClassName().contains(className)) { // 说明它已经启动了
                return true;
            }
        }
        return false;
    }

    public static String getDeviceInfo() {
        if (TextUtils.isEmpty(App.mAppConfig.deviceId)) {
            String deviceId = SharedPref.getInstance(App.getApplication()).getString(Constant.DEVICE_ID, "");
            App.mAppConfig.deviceId = deviceId;
        }

        HashMap<String, String> params = new HashMap<>();
        params.put("platform", "1");
        params.put("device_model", Build.BRAND + " " + Build.MODEL);
        params.put("device_system", Build.VERSION.RELEASE);
        params.put("device_id", App.mAppConfig.deviceId);
        return JSONObject.toJSONString(params);
    }
}
